package cn.changeforyou.analyzer.combine.orm.compare;

import cn.changeforyou.analyzer.dbParse.model.MysqlTableInfo;
import cn.changeforyou.analyzer.dbParse.model.MysqlTableInfo.ColumnInfo;
import cn.changeforyou.analyzer.javaparse.model.EntityModel;
import cn.changeforyou.analyzer.javaparse.model.EntityModel.FieldInfo;
import cn.changeforyou.utils.string.StringUtils;
import java.util.ArrayList;
import java.util.List;
import javax.validation.constraints.NotNull;
import lombok.Data;

/**
 * 实体类和表比较器
 *
 * @author zhyu
 * @date 2021/2/18 11:35
 */
public class EntityAndTableComparetor {

    public @NotNull EntityAndTableCompareResult compare(MysqlTableInfo tableInfo, EntityModel entityModel) {
        EntityAndTableCompareResult entityAndTableCompareResult = new EntityAndTableCompareResult();
        if (null == tableInfo && null == entityModel) {
            return entityAndTableCompareResult;
        } else if (null == entityModel) {
            for (ColumnInfo columnInfo : tableInfo.getColumnInfos()) {
                entityAndTableCompareResult.addColumnInfo(columnInfo);
            }
        } else {
            ArrayList<ColumnInfo> columnInfos = new ArrayList<>(tableInfo.getColumnInfos());
            ArrayList<FieldInfo> entityInfos = new ArrayList(entityModel.getFieldInfos());
            for (int i = columnInfos.size() - 1; i > -1; i--) {
                ColumnInfo columnInfo = columnInfos.get(i);
                for (int j = entityInfos.size() - 1; j > -1; j--) {
                    FieldInfo fieldInfo = entityInfos.get(j);
                    if (StringUtils.nameIsEquals(fieldInfo.getName(), columnInfo.getColumnName(), true, true)) {
                        columnInfos.remove(columnInfo);
                        entityInfos.remove(fieldInfo);
                        fieldInfo.setColumn(columnInfo.getColumnName());
                        break;
                    }
                }
            }
            for (ColumnInfo columnInfo : columnInfos) {
                entityAndTableCompareResult.addColumnInfo(columnInfo);
            }
            for (FieldInfo entityInfo : entityInfos) {
                entityAndTableCompareResult.addFieldInfo(entityInfo);
            }
        }
        entityAndTableCompareResult.setEntityModel(entityModel);
        entityAndTableCompareResult.setMysqlTableInfo(tableInfo);
        return entityAndTableCompareResult;
    }


    @Data
    public static class EntityAndTableCompareResult {

        private List<ColumnInfo> dbs;
        private List<FieldInfo> entitys;
        private MysqlTableInfo mysqlTableInfo;
        private EntityModel entityModel;

        public EntityAndTableCompareResult() {
            dbs = new ArrayList<>();
            entitys = new ArrayList<>();
        }

        public void addColumnInfo(ColumnInfo columnInfo) {
            dbs.add(columnInfo);
        }

        public void addFieldInfo(FieldInfo fieldInfo) {
            entitys.add(fieldInfo);
        }
        
        public boolean isEmpty(){
            return dbs.isEmpty() && entitys.isEmpty();
        }
    }
}
